package com.ja.planclass.controller;

import com.ja.planclass.model.PlanClass;
import com.ja.planclass.model.PlanClassAddVo;
import com.ja.planclass.model.PlanClassSelectVo;
import com.ja.planclass.model.PlanClassUpdateVo;
import com.ja.planclass.mapper.PlanClassMapper;
import com.ja.planclass.service.PlanClassService;
import com.ja.util.AllRecords;
import com.ja.util.ResultBuildVo;
import com.ja.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by 86151 on 2019/4/22.
 */

@RestController
@RequestMapping("planCategory")
public class PlanClassController {
    @Autowired
    private PlanClassService planClassService;

    @PostMapping("add")
    @CrossOrigin
    public ResultVo addPlanClass(@RequestBody @Valid PlanClassAddVo planClassAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return  ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
        }
        return planClassService.addPlanClass(planClassAddVo);

    }
    @PostMapping("updatePlanClass")
    public String updatePlanClass(@RequestBody @Valid PlanClassUpdateVo planClassUpdateVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return bindingResult.getFieldError().getDefaultMessage();
        }
//     planClassService.addPlanClass(planClassAddVo);
        return "SUCCESS";
    }

    @PostMapping("queryPage")
    @CrossOrigin
    public ResultVo<AllRecords> queryPage(@RequestBody PlanClassSelectVo planClassSelectVo){
        return  planClassService.queryPage(planClassSelectVo);
    }

    @Autowired
    private PlanClassMapper planClassMapper;
    @GetMapping("/testMapper")
    public List<PlanClass> testMapper(@RequestParam("codeList")List<String> codeList){
       return planClassMapper.queryPlanClassByCodeList(codeList);
    }
    @PostMapping("/queryPageTest")
    public List<PlanClass> queryPageTest(@RequestBody PlanClassSelectVo planClassSelectVo){
       return planClassMapper.queryPageTest(planClassSelectVo);
    }
}
